{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "immediate-observation",
   "metadata": {},
   "source": [
    "# K8S Resource Test\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "unexpected-pacific",
   "metadata": {},
   "outputs": [],
   "source": [
    "SCHEDULER_IP=!kubectl get svc seldon-scheduler -n seldon-mesh -o jsonpath='{.status.loadBalancer.ingress[0].ip}'\n",
    "SCHEDULER_IP=SCHEDULER_IP[0]\n",
    "import os\n",
    "os.environ['SCHEDULER_IP'] = SCHEDULER_IP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "experimental-partner",
   "metadata": {},
   "outputs": [],
   "source": [
    "MESH_IP=!kubectl get svc seldon-mesh -n seldon-mesh -o jsonpath='{.status.loadBalancer.ingress[0].ip}'\n",
    "MESH_IP=MESH_IP[0]\n",
    "import os\n",
    "os.environ['MESH_IP'] = MESH_IP"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "about-museum",
   "metadata": {},
   "source": [
    "## Show ServerConfigurations Available"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "extreme-complement",
   "metadata": {},
   "outputs": [],
   "source": [
    "!kubectl get serverconfig -n seldon-mesh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "awful-narrow",
   "metadata": {},
   "outputs": [],
   "source": [
    "!kubectl get serverconfig -n seldon-mesh mlserver -o jsonpath='{.spec}' | jq ."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "complimentary-morrison",
   "metadata": {},
   "source": [
    "## Deploy MLServer Instance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "peripheral-diabetes",
   "metadata": {},
   "outputs": [],
   "source": [
    "!cat ../../operator/samples/servers/mlserver1.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "twenty-western",
   "metadata": {},
   "outputs": [],
   "source": [
    "!kubectl create -f ../../operator/samples/servers/mlserver1.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "organizational-treasurer",
   "metadata": {},
   "outputs": [],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s server --all -n seldon-mesh"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "completed-blackjack",
   "metadata": {},
   "source": [
    "## Deploy Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "extended-seventh",
   "metadata": {},
   "outputs": [],
   "source": [
    "!cat ../../operator/samples/models/sklearn-iris-gs.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "respected-switzerland",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "!kubectl create -f ../../operator/samples/models/sklearn-iris-gs.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "tamil-median",
   "metadata": {},
   "outputs": [],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s model --all -n seldon-mesh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "threaded-spread",
   "metadata": {},
   "outputs": [],
   "source": [
    "!kubectl get model iris -n seldon-mesh -o jsonpath='{.status}' | jq ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "caroline-europe",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "!curl -v http://${MESH_IP}/v2/models/iris/infer -H \"Content-Type: application/json\" -H \"seldon-model: iris\"\\\n",
    "        -d '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "sophisticated-washington",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "!kubectl get server mlserver1 -n seldon-mesh -o jsonpath='{.status}' | jq ."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "supreme-baseball",
   "metadata": {},
   "source": [
    "## Deploy Second Server"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "behavioral-mexican",
   "metadata": {},
   "outputs": [],
   "source": [
    "!cat ../../operator/samples/servers/mlserver2.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "preceding-horror",
   "metadata": {},
   "outputs": [],
   "source": [
    "!kubectl create -f ../../operator/samples/servers/mlserver2.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "secret-least",
   "metadata": {},
   "outputs": [],
   "source": [
    "!kubectl wait --for condition=ready --timeout=300s server --all -n seldon-mesh"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "honey-lighter",
   "metadata": {},
   "source": [
    "## Delete First Server"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "excellent-abraham",
   "metadata": {},
   "outputs": [],
   "source": [
    "!kubectl delete -f ../../operator/samples/servers/mlserver1.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dutch-philip",
   "metadata": {},
   "outputs": [],
   "source": [
    "!kubectl get server mlserver2 -n seldon-mesh -o jsonpath='{.status}' | jq ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "patient-emperor",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "!curl -v http://${MESH_IP}/v2/models/iris/infer -H \"Content-Type: application/json\" -H \"seldon-model: iris\"\\\n",
    "        -d '{\"inputs\": [{\"name\": \"predict\", \"shape\": [1, 4], \"datatype\": \"FP32\", \"data\": [[1, 2, 3, 4]]}]}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "imperial-enhancement",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "!kubectl delete -f ../../operator/samples/models/sklearn-iris-gs.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "indirect-error",
   "metadata": {},
   "outputs": [],
   "source": [
    "!kubectl get server mlserver2 -n seldon-mesh -o jsonpath='{.status}' | jq ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "colonial-dakota",
   "metadata": {},
   "outputs": [],
   "source": [
    "!kubectl delete -f ../../operator/samples/servers/mlserver2.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "environmental-collar",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
